package com.woniuxy.common.jwt.util;

import cn.hutool.core.lang.UUID;
import cn.hutool.jwt.JWTPayload;
import cn.hutool.jwt.JWTUtil;
import cn.hutool.jwt.signers.JWTSigner;
import cn.hutool.jwt.signers.JWTSignerUtil;
import org.springframework.core.io.ClassPathResource;
import org.springframework.security.rsa.crypto.KeyStoreKeyFactory;

import java.security.KeyPair;
import java.util.HashMap;
import java.util.Map;


public class JwtTemplate {

    /**
     * 秘钥文件的路径
     */
    private String path;

    /**
     * 秘钥库的口令
     */
    private String keyStoreSecurity;

    /**
     * 秘钥的口令
     */
    private String keySecurity;

    private String alias = "jwt";

    public JwtTemplate() {
    }

    public JwtTemplate(String path, String keyStoreSecurity, String keySecurity) {
        this.path = path;
        this.keyStoreSecurity = keyStoreSecurity;
        this.keySecurity = keySecurity;
    }

    public JwtTemplate(String path, String keyStoreSecurity, String keySecurity, String alias) {
        this.path = path;
        this.keyStoreSecurity = keyStoreSecurity;
        this.keySecurity = keySecurity;
        this.alias = alias;
    }

    public static void main(String[] args) {
        JwtTemplate template = new JwtTemplate("jwt.jks", "123456", "123456");
        Map map = new HashMap();
        map.put("userId", "1001");
        String token = template.createToken(map);
        System.out.println(token);
        //String token = "hello";// JWT -> JWS  JWE   (JWS : 3个部分)
        boolean verify = template.verify(token);
        System.out.println(verify);
        String userId = (String) template.getClaims(token, "userId");
        System.out.println(userId);
    }

    /**
     * 获取秘钥对
     *
     * @return
     */
    public KeyPair getKeyPair() {
        //读取秘钥库文件，参数为文件的路径，ClassPathResource表示在类路径下面加载文件；以及秘钥库的密码
        KeyStoreKeyFactory factory = new KeyStoreKeyFactory(new ClassPathResource(path), keyStoreSecurity.toCharArray());
        //获取秘钥对对象，参数为秘钥库的别名 及 密码
        return factory.getKeyPair(alias, keySecurity.toCharArray());
    }

    /**
     * 获取JWT签名
     *
     * @return
     */
    public JWTSigner getJWTSigner() {
        //生成秘钥签名
        return JWTSignerUtil.createSigner("RSA", getKeyPair());
    }

    /**
     * 生成token ，不指定过期时间，通过redis来处理token的过期时间
     *
     * @param payload
     * @return
     */
    public String createToken(Map<String, Object> payload) {
        payload.put(JWTPayload.JWT_ID, UUID.randomUUID());
        //payload.put(JWTPayload.ISSUED_AT, DateUtil.date());
        return JWTUtil.createToken(payload, getJWTSigner());
    }

    /**
     * 解析token，获取载荷数据
     *
     * @param token
     * @return
     */
    public JWTPayload parseToken(String token) {
        return JWTUtil.parseToken(token).getPayload();
    }

    /**
     * 获取载荷中的声明（存储的数据）
     *
     * @param token
     * @param name
     * @return
     */
    public Object getClaims(String token, String name) {
        return this.parseToken(token).getClaim(name);
    }

    /**
     * 验证token
     *
     * @param token
     * @return
     */
    public boolean verify(String token) {
        return JWTUtil.verify(token, getJWTSigner());
    }


}
